﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

[DirectMethodProxyID(IDMode = DirectMethodProxyIDMode.None)]
public partial class User_LetterDetail : System.Web.UI.Page
{
    UsersRepository URepo = new UsersRepository();
    UserMailbox MailRepo = new UserMailbox();
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int UserId = URepo.GetUserIdByName(_user["username"]);
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                try
                {
                    int Id = int.Parse(Request.QueryString["ID"]);
                    var mailObj = MailRepo.GetEmailById(UserId, Id);
                    if (mailObj != null)
                    {
                        lblSubject.Text = mailObj.MailBox_Subject;
                        lblFrom.Text = mailObj.User.Users_Email;
                        lblReceiveDate.Text = Convert.ToDateTime(mailObj.MailBox_ReceiveDate).ToShortDateString();
                        ltrContent.Text = mailObj.MailBox_Content;
                        if (mailObj.MailBox_Status == (short)Mailbox.Outbox)
                            btnReply.Visible = false;
                    }
                    else
                        Response.Redirect(ResolveUrl("~/Mailbox.html"));
                }
                catch
                {
                }
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int Id = int.Parse(Request.QueryString["ID"]);
        int UserId = URepo.GetUserIdByName(_user["username"]);
        if (MailRepo.DeteteMailbox(UserId, Id))
        {
            Response.Redirect(ResolveUrl("~/Mailbox.html"));
        }
    }

    [DirectMethod]
    public static string ShowAlertDeleteMail()
    {
        return "Tin nhắn đã được xóa";
    }

    protected void GotoReplyPage(object sender, EventArgs e)
    {
        int MailId = int.Parse(Request.QueryString["ID"]);
        Response.Redirect(ResolveUrl("~/Tra-loi/" + MailId + ".html"));
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl("~/Mailbox.html"));
    }
}